package com.module.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.module.framework.domain.LoginUser;
import com.module.framework.domain.User;
import com.module.framework.mapper.MenuMapper;
import com.module.framework.mapper.ProductMapper;
import com.module.framework.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author JiHuiQin
 * @Date 2022/2/14 21:11
 * @Version 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private MenuMapper menuMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 查询用户的信息
		LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
		// 数据库中的用户名和传递的用户名
		queryWrapper.eq(User::getUserName, username);
		User user = userMapper.selectOne(queryWrapper);
		// 如果没有查询到用户就跑出异常
		if (Objects.isNull(user)) {
			throw new RuntimeException("用户不存在");
		}

		// 查询用户的权限信息
		// List<String> list = menuMapper.selectPermsByUserId(user.getId());

		//如果有对应的用户，把用户封装成UserDetails
		return new LoginUser(user);
	}
}
